package com.lgc.demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class QQTest {
    public static void main(String[] args) throws Exception {
        // 设置Chrome浏览器驱动路径
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");


        WebDriver driver = oponChrome();

        // 打开QQ邮箱登录页面
        driver.get("https://mail.qq.com/");

        // 点击"qq登录"
        WebElement qqLoginTab = driver.findElement(By.id("QQMailSdkTool_login_loginBox_tab_item_qq"));
        qqLoginTab.click();

        driver.switchTo().frame(driver.findElement(By.xpath("//a[id='QQMailSdkTool_login_loginBox_qq_iframe']")));

        driver.switchTo().frame(driver.findElement(By.xpath("//a[id='ptlogin_iframe']")));


        // 点击"密码登录"
//        WebElement passwordLoginTab = driver.findElement(By.id("switcher_plogin"));
//        passwordLoginTab.click();

        WebDriverWait webDriverWait = new WebDriverWait(driver, Duration.ofSeconds(5));
        WebElement element = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@id='switcher_plogin']")));
        element.click();

        // 输入QQ号码和密码，并点击登录按钮
        driver.switchTo().frame("login_frame");
        WebElement qqNumberInput = driver.findElement(By.id("u"));
        qqNumberInput.sendKeys("2960853985");  // 输入QQ号码

        WebElement passwordInput = driver.findElement(By.id("p"));
        passwordInput.sendKeys("sql3306!");  // 输入密码

        Thread.sleep(5000); //停止10s， 手动 验证

        WebElement loginButton = driver.findElement(By.id("login_button"));
        loginButton.click();  // 点击登录按钮

        // 等待登录成功并跳转到邮箱主页
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait.until(ExpectedConditions.urlContains("mail.qq.com/cgi-bin/frame_html"));

        // 点击写信按钮进入写信页面
        WebElement composeButton = driver.findElement(By.id("composebtn"));
        composeButton.click();

        // 输入收件人、主题和邮件内容，并发送邮件
        driver.switchTo().frame("mainFrame");

        WebElement recipientInput = driver.findElement(By.id("toAreaCtrl"));
        recipientInput.click();
        recipientInput.sendKeys("222");  // 输入收件人

        WebElement subjectInput = driver.findElement(By.id("subject"));
        subjectInput.sendKeys("测试邮件");  // 输入主题

        WebElement contentFrame = driver.findElement(By.xpath("//iframe[contains(@id, 'QMEditor')]"));
        driver.switchTo().frame(contentFrame);

        WebElement contentInput = driver.findElement(By.xpath("/html/body"));
        contentInput.sendKeys("这是一封测试邮件");  // 输入邮件内容

        // 切换回主页面
        driver.switchTo().defaultContent();

        // 发送邮件
        WebElement sendButton = driver.findElement(By.id("toolbar_send"));
        sendButton.click();

        // 关闭浏览器
        wait.until(ExpectedConditions.urlContains("mail.qq.com/cgi-bin/frame_html"));  // 等待邮件发送成功
        driver.quit();
    }


    private static ChromeDriver oponChrome() {
        // 设置驱动程序的路径
        System.setProperty("webdriver.chrome.driver", "E:\\桌面 1\\selenium4QQDemo\\src\\main\\resources\\chromedriver.exe");
        // 创建Edge浏览器驱动
        ChromeDriver driver = new ChromeDriver();
//        driver.get("https://www.baidu.com");

        return driver;
    }
}
